package com.tencent.cloud.iov.kernel;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shockcao
 * @date 2018/04/24
 */
public class IovKernel {

    private static Context sContext = null;

    /**
     * 厂商ID
     */
    private static int sCompanyId = 0;

    /**
     * qq关联登录appId
     */
    private static String sQQAppId = "";

    /**
     * weChat关联登录appId
     */
    private static String sWeChatAppId = "";

    /**
     * 状态码监听
     */
    private static List<HttpStatusCodeListener> sHttpStatusCodeListeners;

    /**
     * * 初始化
     *
     * @param context applicationContext
     */
    public static void init(Context context) {
        sContext = context.getApplicationContext();
    }

    /**
     * 设置厂商ID
     *
     * @param companyId
     */
    public static void setCompanyId(int companyId) {
        sCompanyId = companyId;
    }

    /**
     * 获取厂商ID
     *
     * @return
     */
    public static int getCompanyId() {
        return sCompanyId;
    }

    /**
     * 设置社交信息
     *
     * @param qqAppId     qq关联登录appId
     * @param weChatAppId weChat关联登录appId
     */
    public static void setSocialInfo(String qqAppId, String weChatAppId) {
        sQQAppId = qqAppId;
        sWeChatAppId = weChatAppId;
    }

    public static String getQQAppId() {
        return sQQAppId;
    }

    public static String getWeChatAppId() {
        return sWeChatAppId;
    }

    public static Context getContext() {
        return sContext;
    }

    /**
     * 添加响应状态码监听器
     *
     * @param listener
     */
    public static void addHttpStatusCodeListener(HttpStatusCodeListener listener) {
        synchronized (IovKernel.class) {
            if (sHttpStatusCodeListeners == null) {
                sHttpStatusCodeListeners = new ArrayList<>();
            }
            if (!sHttpStatusCodeListeners.contains(listener)) {
                sHttpStatusCodeListeners.add(listener);
            }
        }
    }

    /**
     * 移除响应状态码监听器
     *
     * @param listener
     */
    public static void removeHttpStatusCodeListener(HttpStatusCodeListener listener) {
        synchronized (IovKernel.class) {
            if (sHttpStatusCodeListeners != null && sHttpStatusCodeListeners.contains(listener)) {
                sHttpStatusCodeListeners.remove(listener);
            }
        }
    }

    /**
     * 处理网络请求状态码
     *
     * @param statusCode
     */
    public static void handleHttpStatusCode(int statusCode) {
        if (sHttpStatusCodeListeners != null) {
            for (HttpStatusCodeListener httpStatusCodeListener : sHttpStatusCodeListeners) {
                httpStatusCodeListener.onHttpStatusCode(statusCode);
            }
        }
    }
}
